package com.huiyuanai.pojo.bill;

import com.huiyuanai.dao.Contact;
import com.huiyuanai.util.BaseUtil;
import com.mysql.cj.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/10/18/018
 */
public class BillPojoImpl implements BillPojo {
    @Override
    public List<Contact> getContact(Connection connection, String productName, String ProviderId, int isPayment, int id) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Object> list = new ArrayList<Object>();
        List<Contact> resultList = new ArrayList<Contact>();

        if (connection != null) {
            String sql = "select b.*, p.proName, p.id as pid from smbms_provider p, smbms_bill b where b.providerId = p.id";
            if (!StringUtils.isNullOrEmpty(productName)) {
                sql += " and b.productName like ?";
                list.add("%" + productName + "%");
            }
            if (!StringUtils.isNullOrEmpty(ProviderId) && (Integer.parseInt(ProviderId) > 0)) {
                sql += " and  p.id = ?";
                list.add(ProviderId);
            }
            if (isPayment > 0) {
                sql += " and b.isPayment = ?";
                list.add(isPayment);
            }
            if (id > 0) {
                sql += " and b.id = ?";
                list.add(id);
            }
            resultSet = BaseUtil.execute(connection, sql, list.toArray(), resultSet, preparedStatement);
            while (resultSet.next()) {
                Contact contact = new Contact();
                contact.setId(resultSet.getInt("id"));
                contact.setBillCode(resultSet.getString("billCode"));
                contact.setProductName(resultSet.getString("productName"));
                contact.setProductDesc(resultSet.getString("productDesc"));
                contact.setProductUnit(resultSet.getString("productUnit"));
                contact.setProductCount(resultSet.getBigDecimal("productCount"));
                contact.setTotalPrice(resultSet.getBigDecimal("totalPrice"));
                contact.setIsPayment(resultSet.getInt("isPayment"));
                contact.setCreatedBy(resultSet.getInt("createdBy"));
                contact.setCreationDate(resultSet.getDate("creationDate"));
                contact.setProviderName(resultSet.getString("proName"));
                contact.setProviderId(resultSet.getInt("pid"));
                resultList.add(contact);
            }
            System.out.println(sql);
        }
        BaseUtil.clostResour(null, preparedStatement, resultSet);
        return resultList;
    }

    @Override
    public int add(Connection connection, Contact contact) throws SQLException {
        PreparedStatement preparedStatement = null;
        List<Object> list = new ArrayList<Object>();
        int result = 0;
        if (connection != null) {
            String sql = "insert into smbms_bill (billCode, productName, productDesc, productUnit, productCount, totalPrice, isPayment, createdBy, creationDate, providerId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            list.add(contact.getBillCode());
            list.add(contact.getProductName());
            list.add(contact.getProductDesc());
            list.add(contact.getProductUnit());
            list.add(contact.getProductCount());
            list.add(contact.getTotalPrice());
            list.add(contact.getIsPayment());
            list.add(contact.getCreatedBy());
            list.add(contact.getCreationDate());
            list.add(contact.getProviderId());
            result = BaseUtil.execute(connection, sql, list.toArray(), preparedStatement);
            System.out.println(sql);
        }
        BaseUtil.clostResour(null, preparedStatement, null);
        return result;
    }

    @Override
    public int del(Connection connection, String id) throws SQLException {
        PreparedStatement preparedStatement = null;
        List<Object> list = new ArrayList<Object>();
        int result = 0;
        if (connection != null) {
            String sql = "delete from smbms_bill where id = ?";
            if (Integer.parseInt(id) > 0) {
                list.add(id);
            }
            result = BaseUtil.execute(connection, sql, list.toArray(), preparedStatement);
        }
        BaseUtil.clostResour(null, preparedStatement, null);
        return result;
    }

    @Override
    public int upDate(Connection connection, Contact contact) throws SQLException {
        PreparedStatement preparedStatement = null;
        List<Object> list = new ArrayList<Object>();
        int result = 0;

        if (connection != null) {
            String sql = "update smbms_bill set billCode = ?, productName = ?, productUnit = ?, productCount = ?, totalPrice = ?, isPayment =?, modifyBy = ?, modifyDate = ? where id = ?";
            list.add(contact.getBillCode());
            list.add(contact.getProductName());
//            list.add(contact.getProductDesc());
            list.add(contact.getProductUnit());
            list.add(contact.getProductCount());
            list.add(contact.getTotalPrice());
            list.add(contact.getIsPayment());
            list.add(contact.getModifyBy());
            list.add(contact.getModifyDate());
            list.add(contact.getId());
            result = BaseUtil.execute(connection, sql, list.toArray(), preparedStatement);
            System.out.println(sql);
            System.out.println(result);
        }
        BaseUtil.clostResour(null, preparedStatement, null);
        return result;
    }
}
